﻿using System;
using System.IO;

namespace Infrastructure.Serialization
{
    public static class DataSerialize
    {
        public static bool SaveData<T>(T data, string path)
        {
            try
            {
                using (TextWriter textWriter = new StreamWriter(path))
                {
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    serializer.Serialize(textWriter, data);
                    textWriter.Close();
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static T LoadData<T>(string path)
        {
            T data = default(T);
            try
            {
                using (TextReader textReader = new StreamReader(path))
                {
                    System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    data = (T)deserializer.Deserialize(textReader);
                    textReader.Close();
                }
            }
            catch (Exception)
            {
            }
            return data;
        }
    }
}
